473,538 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

making a div a link

111 New Member
I need to make a whole div, become a link.

i currently have the code
Expand|Select|Wrap|Line Numbers
  1. <a href="temp.html"><div>some text</div></a>
but this does not validate as xhtml strict.

putting the link in the div will only make the text a link and not the whole div.
the reason i've got the div in there is becuase i use it to create a box and add a colored background to this.

I also need to be able to achieve the same thing, with a list item.
e.g. I currently have
Expand|Select|Wrap|Line Numbers
  1. <ul>
  2. <a href="temp.html"><li>item 1</li></a>
  3. <a href="temp2.html"><li>item 2</li></a>
  4. <a href="temp3.html"><li>item 3</li></a>
  5. </ul>
which does not validate
Oct 17 '07 #1
25 131717
Death Slaught
1,137 Top Contributor
I need to make a whole div, become a link.

i currently have the code
<a href="temp.html"><div>some text</div></a>

but this does not validate as xhtml strict.

putting the link in the div will only make the text a link and not the whole div.
the reason i've got the div in there is becuase i use it to create a box and add a colored background to this.

I also need to be able to achieve the same thing, with a list item.
e.g. I currently have
<ul>
<a href="temp.html"><li>item 1</li></a>
<a href="temp2.html"><li>item 2</li></a>
<a href="temp3.html"><li>item 3</li></a>
</ul>

which does not validate
It doesn't validate because it should be like this:


[HTML]<div><a href="temp.html">some text</a></div>[/HTML]

[HTML]<ul>
<li><a href="temp.html">item 1</a></li>
<li><a href="temp2.html">item 2</a></li>
<li><a href="temp3.html">item 3</a></li>
</ul>[/HTML]

I don't know where your learning from, but I would stop. Try here It's a pretty good tutorial on HTML.


Hope it helps, Death
Oct 17 '07 #2
mohaakilla51
39 New Member
he knows that is how it should be death, but he wants to make the ENTIRE div a link, which I am not sure how to do, unless you went with Javascript, at which point it would be
[html]
<div OnClick="document.location='http://google.com'"><noscript><a href="http://google.com"></noscript>some Text<noscript></a></noscript></div>
<!--THus, you will get divs that are links, unless the client has no javascript, which will cause the text to be links instead.-->
<!--the same for your lists -->
<ul>
<li OnClick="document.location='http://yahoo.com'"><noscript><a href="http://yahoo.com"></noscript>More Text<noscript></a></noscript></li>
</ul>
[/html]
Does that help? I cannot think of an XHTML strict way to do it otherwise...

Oh, and w3schools.com is an excellent source for HTML, CSS, and PHP (IMHO), although I don't think you need it...
Oct 18 '07 #3
drhowarddrfine
7,435 Recognized Expert Expert
A div cannot be made into a link. It is a structural element, and block level, and is not content by itself. <a> is inline only and will not work on block level elements.
Oct 18 '07 #4
nitinpatel1117
111 New Member
yes i understand that the <a> is inline and <div> is block element.

and hence <div> can't reside in the <a>, but a while back i read something about making the <a> a block element and <div> an inline element, by using the css --> display:block & display:inline respectively. I tried this way but it doesn't seem to validate either.

Can't remember where i read it, it was a while back, so can't remember if i'm doing it the right way.

The div that i have represents a small box on the screen, this box contains an image and a <hr> tag and some text. The whole things needs to be a link.

i'm already using the javascript onclick method on the div, but i wanted to do it without using javascipt to make it more SEO friendly.

has anyone ever tried anything similar, or is there a better way of doing it, to acheive the same resutls.
Thanks
Oct 18 '07 #5
Death Slaught
1,137 Top Contributor
yes i understand that the <a> is inline and <div> is block element.

and hence <div> can't reside in the <a>, but a while back i read something about making the <a> a block element and <div> an inline element, by using the css --> display:block & display:inline respectively. I tried this way but it doesn't seem to validate either.

Can't remember where i read it, it was a while back, so can't remember if i'm doing it the right way.

The div that i have represents a small box on the screen, this box contains an image and a <hr> tag and some text. The whole things needs to be a link.

i'm already using the javascript onclick method on the div, but i wanted to do it without using javascipt to make it more SEO friendly.

has anyone ever tried anything similar, or is there a better way of doing it, to acheive the same resutls.
Thanks
Depending on where your putting the text you can just make the image a link.......


- Death
Oct 18 '07 #6
jp500
1 New Member
It can be done with javascript, like this:

<div id="xxxxx" OnClick="javascript: window.open('http://www.xxxxx')">

In the syleheet add:
cursor: pointer;
to your div...
Nov 5 '07 #7
drhowarddrfine
7,435 Recognized Expert Expert
I didn't know <div> had "onclick" as an attribute. Which makes me want to look further into making a div as a link. Just wrapping <a> around a <div> will work but the validator complains about it.
Nov 5 '07 #8
ineedasn
1 New Member
Expand|Select|Wrap|Line Numbers
  1. #list li a{
  2. width: 100px;
  3. display:block;
  4. }

I guess
display:block;
seems to do the trick...
technically it wouldn't be a div as a link but same difference...
Sep 18 '08 #9
Annalyzer
122 New Member
The book I'm reading suggests setting the display property of the anchor to block.

Expand|Select|Wrap|Line Numbers
  1. a {
  2.   display: block;
  3.   width: 5em;
  4.   padding: 0.2em;
  5.   line-height: 1.5;
  6.   background-color: #990099;
  7.   text-decoration: none;
  8.   text-align: center;
  9. }
This doesn't use a <div> element, but does create the desired box and allows for a background color.
Sep 18 '08 #10
jermouri
1 New Member
in some cases, you can use something else instead of a div to achieve the same effect while pleasing the validator gods. for instance, a span or an img could work, and they are inline elements.
for ul li, what you can do is the following:
<style>li:hover {background:#ccc;}</style>
<ul>
<li><a>first choice</a></li>
<li><a>second choice</a></li>
</ul>
the link proper would be on the text inside the li but the li block will react to the cursor.
Dec 9 '09 #11
hsriat
1,654 Recognized Expert Top Contributor
The div that i have represents a small box on the screen, this box contains an image and a <hr> tag and some text. The whole things needs to be a link.
I believe there's no point of inserting an image and <hr> inside a link. A good alternative can be if you use display: block (as you already know), then use the image as a background-image with no-repeat, stuff a padding-left (or which ever side you want the image to be) according to the size of the image, and then finally replace the <hr> with border-bottom.
If you want an encapsulating border for this whole thing, pack it inside a div tag (w3c'cally legal) with required border and a small padding.
Dec 10 '09 #12
kovik
1,044 Recognized Expert Top Contributor
One: The OP wants it to validate. CSS doesn't cause HTML to validate. A block element cannot be placed in an inline element, regardless of the CSS. So, placing the DIV inside of the A won't work.

Two: Part of the lack of validation is that the LI element should be the only direct child of the UL element. The A element cannot be the direct child of the UL element.

Three: ~jermouri has it right, to a point. However, giving the block element a "cursor:pointer" style doesn't address the fact that clicking it will accomplish nothing.


So, the answer that you are looking for is to simply use an element other than DIV. For validation, you'll want to use an inline element. You could use SPAN, as suggested by ~jermouri, or you could use an element with more meaning. I, personally, am more likely to choose STRONG, EM, B, or I before resorting to SPAN.

You can then use CSS to make these elements block elements. The reason that hte HTML must validate without the CSS is because the HTML should be able to stand on its own without the CSS. Without CSS, block elements inside of inline elements makes no sense.


So, the final result is:

Expand|Select|Wrap|Line Numbers
  1. <a href="temp.html"><b>some text</b></a>
Expand|Select|Wrap|Line Numbers
  1. <ul>
  2.     <li><a href="temp.html"><b>item 1</b></a></li>
  3.     <li><a href="temp2.html"><b>item 2</b></a></li>
  4.     <li><a href="temp3.html"><b>item 3</b></a></li>
  5. </ul>
Expand|Select|Wrap|Line Numbers
  1. a,
  2. a b {
  3.   display: block;
  4. }
Dec 11 '09 #13
drhowarddrfine
7,435 Recognized Expert Expert
A lot of action for a 2-year old thread.
Dec 11 '09 #14
kovik
1,044 Recognized Expert Top Contributor
... Wish I had noticed. Lock?
Dec 11 '09 #15
I use span instead of div.
Seems to work fine and validate XHTML Strict....

<a href="http://"><span class="" style="height:73px; width:150px;"></span></a>

Been using it for some time now since I found that using the div inside the link tags doesnt validate.
No problems whatsoever so far.
Sep 27 '10 #16
kovik
1,044 Recognized Expert Top Contributor
Double-necro'd. And with absolutely nothing new to add. Bravo.
Sep 27 '10 #17
Hi everyone,

Two possible solutions:

1) Put the link inside the <div> and wrap all inline elements within the link in <object> tags. So like this:

Expand|Select|Wrap|Line Numbers
  1. <div>
  2. <a href="#">
  3. <object><h3>Header text here</h3></object>
  4. <img src="#">
  5. <object><p>Paragraph text here</p></object>
  6. </a>
  7. </div>
This validates and everything within the <a> tag now becomes clickable. You don't have to put blocklevel elements (like <img>) within <object> tags to make this validate.

2) You can put your link inside a <span>, which is inside the <div>. You can then add a transparent image the size of the block you want to be clickable to the link (a completely transparent png or gif will work) and position this absolutely over the <div>. So like this:

Expand|Select|Wrap|Line Numbers
  1. <div>
  2. <span><a href="#"><img src="transparent.png" width="200px" height="150px"</a></span>
  3. <h3>Header text here</h3>
  4. <img src="#">
  5. <p>Paragraph text here</p>
  6. </div>
To make it work you give the <span> the same width and height (using CSS) as your transparent image. So the CSS for the <span> would look something like this:

Expand|Select|Wrap|Line Numbers
  1. span {
  2. width:200px;
  3. height:150px;
  4. position:absolute;
  5. }
In order for the absolute positioning to work you need to position the containing <div> relatively. So the CSS for the containing <div> would look something like this:

Expand|Select|Wrap|Line Numbers
  1. div {
  2. position:relative;
  3. }
Hope this helps...
Oct 8 '10 #18
drhowarddrfine
7,435 Recognized Expert Expert
Maybe we can continue this thread another 3 years from now.
Oct 8 '10 #19
Simple answer, put a blank transparent PNG inside the div, set it to the same height and width as the div and link the image.

It's a bit heavy handed, but it works.
Oct 12 '10 #20
kovik
1,044 Recognized Expert Top Contributor
Seriously...? Just put an inline-level element in the anchor and use CSS to make it a block. It's not nearly as complicated as you guys are making it. When someone has CSS turned off, it means that they don't care what the page looks like. They don't need block-element links.

Now please... Let the thread die?
Oct 13 '10 #21
luciminea
2 New Member
The problems are simple. YES a div can be a link, those who doesn't know it yet, should learn some more. The html code for this is as simple as you wrote initialy:
Expand|Select|Wrap|Line Numbers
  1. <a href="#"><div class="someclass"><div></a>
But for this to work, should have in the .css file as a rule for you div, the folowing: a given height, a given width, and the rule display:block, but not for <a> element, but for "someclass" element.

Do that and I gurantee this work without a single problem.

For the list problem (ul and li) I wouldn't sugest you use this technique, better explain exactly what you want to achieve, and we'll help you with the right solution.
Mar 2 '12 #22
drhowarddrfine
7,435 Recognized Expert Expert
Almost five years ago, WHEN THIS THREAD ORIGINALLY STARTED, trying to make a div a link was invalid. It has only been possible and valid to do with HTML5.
Mar 2 '12 #23
luciminea
2 New Member
Oh my god ! My bad, I didn't look at the date, my apology.
Mar 2 '12 #24
kovik
1,044 Recognized Expert Top Contributor
The thread that never dies. At least it gives me a reason to check up on Bytes and read how much of a **** I used to be. Good times, man. Good times.
Mar 2 '12 #25
rybo111
1 New Member
@drhowarddrfine
No worries.

HTML:
Expand|Select|Wrap|Line Numbers
  1. <div class="inner-link">
  2.   Go to <a href="https://google.com">Google</a>
  3. </div>
jQuery:
Expand|Select|Wrap|Line Numbers
  1. $(".inner-link").click(function() {
  2.   window.location = $(this).find("a").attr("href");
  3. });
Nov 17 '13 #26

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3044
by: Unregistered | last post by:
I'm making an Intranet site for someone who is not very familiar wit making websites, thus I would like to make it as simple as possible fo her when she needs to update it. The intranet site uses frames - one menu frame that is always present and one frame into which the content is loaded. The intranet sit features many, many links to both...
7
3778
by: bill | last post by:
Is there any way to make a link (as marked by an <A>-tag, that is), behave exactly like a submit button? Many thanks! -bill
7
22299
by: ravey | last post by:
I would like to use a Form "button" (or "input", either one) within an anchor, instead of using text or creating a graphic. This works fine in all browsers but IE, but IE, while showing the correct URL in the status line and the tooltip, DOESN'T initiate the link. The code is essentially: <a href="http://foo.com/whatever.htm"> <input...
6
2027
by: Ozz | last post by:
Hi there, I have a link on my web page. When clicked, opens up a pdf file that is stored on my server. Every file is specific to a user's user name and I don't want users to see each other's files. For example: When User1 clicks on the link, it opens up http://mydomain.com/files/user1.pdf and when User2 clicks on the link, it opens up...
3
2989
by: piscogirl | last post by:
Hi there, I am about to build a small db in Access. Among the tables I plan to have are a Person table, an Event table, and an EventRegistration table. The EventRegistration table will contain the information gathered when someone registers for one of our courses. Some of the information will be gathered from each of the other two...
2
1392
by: Nehmo | last post by:
On http://sketchup.google.com/gsu5vtviewer.html#id=27 The video for the Rotate Tool lesson works okay when I click on it, but I want to link directly to it. Looking at the source of the page there's a section corresponding to the Rotate video link: videos = {title: "Rotate Tool", link: "Mod_B2-rotate_tool.swf",
11
8110
by: mosscliffe | last post by:
I am trying to create a link to a file, which I can then use in an HTML page. The system is Linux on a hosted web service, running python 2.3. Other than that I have no knowledge of the system. The link is created OK, but when I try to use it as filename for the IMG TAG, it does not get displayed. The page source of the created page is...
1
1768
by: nicky77 | last post by:
Hi, apologies in advance is this may be a nonsensical ramble, but I'm hoping someone can give me some advice on how to solve the following problem: I'm developing a site where the main content is loaded dynamically (and using a fade effect) into a placeholder div using AJAX. This is all working fine, however the tricky part for me is making...
7
5704
by: Steve Swift | last post by:
How close can you get to making a submit button look like a link? Answers to the inevitable questions: 1. Because I don't want my email address (which would have to be part of the URL in a real link) becoming obviously visible. 2. Because I'm limited for space and even a styled button takes too much room. --
12
2812
by: prashant | last post by:
hi, i am trying to create an xml tag ref to hold link.php?id=1; or link.php?id=2 and so on. I want the links for all the fields(id) in the databse so that when i call them in my html page for google maps i have the whole link.The tag in the php page i am using is echo 'ref="link.php?id='.$row . '"'; the tag in html is <a href="+ref+">"...
0
7365
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7536
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7287
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5829
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4852
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3353
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.